import { ZResponsePageDataResult } from "@/utils/http/response";
import { octpHttp } from "..";
import { PageRequest } from "../models/base";
import { Commodity } from "../models/commodity";

const url = (path: string) => `/commodity/${path}`;
const request = octpHttp.request;

export const getCommodityById = (id: number) =>
  request<Commodity>({
    url: url("index"),
    method: "get",
    params: { id },
  });

export interface GetCommodityListParams extends PageRequest {
  name?: string;
}

export const getCommodityList = (params: GetCommodityListParams) =>
  request<ZResponsePageDataResult<Commodity>>({
    url: url("list"),
    method: "get",
    params,
  });

export const deleteCommodityById = (id: number) =>
  request({
    url: url("delete"),
    method: "delete",
    params: { id },
  });

// self

export interface CommodityParams {
  name: string;
  price: string;
  description: string;
  photos?: number[];
}

export const updateCommodityInfo = (params: Partial<CommodityParams>) =>
  request(
    {
      url: url("update"),
      method: "post",
      params,
    },
    { carryToken: true }
  );

export const provideCommodity = (params: CommodityParams) =>
  request(
    {
      url: url("create"),
      method: "post",
      params,
    },
    { carryToken: true }
  );

export const consumeCommodity = (id: number) =>
  request(
    {
      url: url("consume"),
      method: "post",
      params: { id },
    },
    { carryToken: true }
  );
